home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 121_01.zip / TL370.C < prev    next >
Text File  |  1993-06-01  |  22KB  |  876 lines

  1. /*
  2. HEADER: CUG 121.??;
  3.  
  4.     TITLE:    tl370 - dumb terminal with file xfer;
  5.     VERSION:    1.0;
  6.     DATE:    01/05/86;
  7.     DESCRIPTION: "TL370 emulates a dumb terminal.  Incoming data may be
  8.         buffered in memory and written to disk, data may be
  9.         transmitted from disk to the modem, or files may be
  10.         formally transferred in a `checksum' mode with automatic
  11.         handshaking and buffering.
  12.         TL370 is a modified version of Telnet v2.3, as distributed
  13.         with v1.46 of the BDS C compiler.  The modification consists
  14.         of the addition of optional XON/XOFF handshaking for file
  15.         transmission.";
  16.     KEYWORDS:    terminal, modem, emulate, file transfer;
  17.     SYSTEM:    CP/M;
  18.     FILENAME:    TL370.C;
  19.     WARNINGS:    "Requires local.c for link.
  20.         The checksum file transfer does not follow the XMODEM
  21.         protocol.
  22.         TL370 assumes that the CP/M console is much faster than the
  23.         modem (e.g. a 1200+ baud console with a 300 baud modem).";
  24.     SEE-ALSO:    CALL.C, TEL.C, TELNET.C;
  25.     AUTHORS:    Leor Zolman, Leo Kenen, Ken Lonquest;
  26.     COMPILERS:    BDS-C 1.50;
  27. */
  28.  
  29. #define TITLE "BDS Telnet version 2.3        (July 1980)"
  30.  
  31. /*
  32.  
  33.     Written by Leor Zolman and Leo Kenen
  34.     December 1979, March 1980, May 1980, July 1980
  35.  
  36.     This version has been modified to obtain all hardware-
  37.     dependent information from bdscio.h, which must    contain
  38.     the correct hardware specifications for your modem port.
  39.     It is also no longer necessary to alter #define statements
  40.     in this file to reflect CP/M system size; the "topofmem()"
  41.     function is now used to determine the amount of memory 
  42.     available for the text collection buffer.
  43.  
  44.     If you intend to use this program for high speed
  45.     (i.e, greater than 300 baud) data transfers, such as
  46.     maybe over RS232 lines between two machines directly,
  47.     then the speed of transfer will be limited by the
  48.     processors involved instead of the baud rate;
  49.     UNDER SUCH CIRCUMSTANCES, A TRANSFER WILL ONLY WORK IF
  50.     THESE TWO CONDITIONS ARE MET:
  51.  
  52.         1) The transfer must always performd in BINARY
  53.            mode, never in TEXT mode, and
  54.         2) The receiving processor must be as fast or
  55.            FASTER than the transmitting processor. That
  56.            is, a 2 MHz machine may transmit to a 4 MHz
  57.            machine at, say, 9600 baud, BUT NOT VICE-
  58.            VERSA.
  59.  
  60.     See the write up by Leo for more details than are
  61.     presented here.
  62.  
  63.     ******************************************
  64.     * Telnet assumes that your CP/M console  *
  65.     * I/O device is much faster than your     *
  66.     * modem. On a 2MHz 8080, the modem can     *
  67.     * be receiving at up to 300 baud as long *
  68.     * as your console whips along (at the     *
  69.     * very least) at about 1200. 4 MHz mach- *
  70.     * ines might be able to get away with     *
  71.     * slower terminals, but not much slower. *
  72.     ******************************************
  73.  
  74. "Telnet" is a program which interacts with a modem to turn
  75.  your microcomputer into a very versatile terminal. Special
  76.  commands are entered to the program by typing the character
  77.  you designate as "SPECIAL", i.e, some character (such as the
  78.  null or ^A ) which you wouldn't be likely to need transmitted,
  79.  and then entering the appropriate command letter. Incoming
  80.  data may be buffered up in RAM memory and dumped to disk
  81.  whenever you desire (via the "o", "d", "c" and "k" commands),
  82.  data may be transmitted from disk to modem (via "t" and "a"),
  83.  or files can be formally transferred in an alternate
  84.  "checksum" mode which handles handshaking and buffering
  85.  automatically when interacting with the same program on
  86.  the other end of the line. During file transfers, you
  87.  may temporarily pause and later resume the transmission
  88.  (via the "p" and "r" commands.) There are also various
  89.  options you can control (see "n", "7", "h" and "l") to
  90.  adapt operation toward the type of file you wish to
  91.  transfer. The "q" command closes the output file (if open)
  92.  and quits to CP/M. The "s" command displays the status of
  93.  the program. "z" clears the console screen. Any other
  94.  command letter (such as, for example, "?") causes a list
  95.  of legal commands to be displayed.
  96.  
  97. In order to transmit or receive files in the checksum
  98. mode, both parties must make sure that their modems are
  99. operating in FULL-DUPLEX. When you are in full duplex,
  100. then what you type will NOT come right back at you from the
  101. modem; the only input you see from the modem is the data
  102. transmitted by the machine on the OTHER end of the line.
  103.  
  104. This program considers "half duplex" to be any situation
  105. in which the data you transmit comes right back at you;
  106. whether it is your modem that is performing the ehoing
  107. or a computer system far away doesn't really matter. In any
  108. case, checksumming and handshaking is not allowed under half-
  109. duplex operation, since erroneous characters would be received.
  110. When you run telnet, it will ask you whether
  111. or not you are in half-duplex, and perform accordingly. If
  112. you switch from half to full or vice-versa while running the
  113. program, use the "h" option to inform telnet of the fact.
  114.  
  115. To perform checksummed file transfer, a connection must
  116. first be established between the two parties. If both
  117. parties are operating in full duplex, one originating and the
  118. other answering, then telnet will both display what each
  119. types to the console and send it to the modem. If a file
  120. then needs to be transferred, then one user would give the
  121. "t" command (to transmit) and the other would give the "o"
  122. command (to open an output file.) If both users indicate
  123. checksum mode (rather than only one specifing checksum mode
  124. which will abort almost immediatly), then telnet will take
  125. it from there and perform the transfer. If the sender
  126. (transmitter) wants to suspend the transfer temporarily and
  127. continue later, he can use the "p" command. When the receiver
  128. sees that transmission has been suspended (when no data has
  129. been sent for a long time), then HE gives the "p" command also,
  130. and both users may type to each other. When ready to resume,
  131. the "r" command must be given by the RECEIVER first, and
  132. then the sender, to prevent data from being lost.
  133.  
  134. */
  135.  
  136. #include <bdscio.h>    /* System, h'ware constants    */
  137. #include <hardware.h>
  138.  
  139. #define SPECIAL 0x18    /*  The character you type to
  140.                signal a Telnet command
  141.                (should be obscure...I use
  142.                 a "control-x")    */
  143.  
  144.  
  145. /*
  146.     The following #defines need not be changed:
  147. */
  148.  
  149. #define    ACK    0x06    /* Ascii ACK for handshaking    */
  150. #define    NAK    0x15
  151. #define    EOT    0x04    /* End of transmission        */
  152. #define    ETX    0x03    /* Abort Transmission        */
  153. #define XON    0x11    /* Enable transmission        */
  154. #define XOFF    0x13    /* Disable transmission        */
  155.  
  156. /*
  157.     External variable declarations:
  158. */
  159.  
  160. char rflag;        /* receiving file open flag    */
  161. char tflag;        /* transmitting file open flag    */
  162. char shflag;    /* ascii(ctrl-S, ctrl-Q) handshaking enabled. */
  163. char chflag;        /* checksumming enabled flag    */
  164. char cflag;        /* text-collection enabled flag */
  165. char pflag;        /* pausing flag         */
  166. char spflag;        /* stripping parity bit flag    */
  167. char lflag;        /* list device enabled flag    */
  168. char nflag;        /* recognizing nulls flag    */
  169. char fflag;        /* true if changing CR-LF's into
  170.                 just CR when transmitting    */
  171. char lastc;        /* last char xmitted        */
  172. char dodflag;        /* true if displaying outging
  173.                data                */
  174. char didflag;        /* true if displaying incoming
  175.                data                */
  176. char hdflag;        /* true if effectively working
  177.                in half-duplex        */
  178. char abortf;        /* true when file I/O aborted    */
  179. char rbuf[BUFSIZ];     /* file I/O buffer for incoming
  180.                data file            */
  181. char tbuf[SECSIZ];     /* sector buffer for file being
  182.                transmitted            */
  183. char rname[20];     /* name of receiving file    */
  184. char tname[20];     /* name of transmitting file    */
  185. int rfd, tfd;        /* file descriptors        */
  186.  
  187. char *cptr;        /* pointer to free space in buf */
  188. unsigned free;        /* number of bytes free in buf    */
  189. int bcount;        /* counts bytes in current block
  190.                when checksumming        */
  191. int scount;        /* Number of sectors
  192.                  sent/received        */
  193. int checksum;        /* the checksum value itself    */
  194. char timoutf;        /* true if time-out happens
  195.                while waiting for modem data    */
  196. char *i;        /* odd-job char pointer     */
  197.  
  198. int dod_sav, did_sav;    /* scratch variables        */
  199.  
  200. unsigned bufspace;    /* # of bytes available for text
  201.                collection buffer in ram    */
  202.  
  203. char *buf;        /* text collection pointer; will
  204.                point to the location just
  205.                after itself            */
  206.  
  207. char toupper();        /* This makes for better code
  208.                than if we let it default
  209.                to "int"            */
  210.  
  211.  
  212. main()
  213. {
  214.     char c, c2;
  215.     int n;
  216.  
  217.     init();
  218.  
  219. loop:    if (abortf) {
  220.         if (rflag) rclose();
  221.         if (tflag) tabort();
  222.         abortf = FALSE;
  223.     }
  224.  
  225.     if (tflag && xmit()) {
  226.             printf("\nTransmission complete.\n");
  227.             close(tfd);
  228.             reset();
  229.             }
  230.     if (abortf) goto loop;
  231.     if (miready()) {
  232.       c = c2 = getmod();
  233.       if (spflag) c &= 0x7f;
  234.       if (tflag) {
  235.         if(c == ETX) {
  236.             printf("Receiver has aborted;\n");
  237.             abortf = TRUE;
  238.             goto loop;
  239.             }
  240.         else if(shflag) {
  241.             if(c == XON) pflag = FALSE ;
  242.             else if(c == XOFF) pflag = TRUE ;
  243.             }
  244.         }
  245.       if (didflag && (c || nflag) && (c != CPMEOF))
  246.             display(c);
  247.       if (cflag && !pflag) {
  248.         if (c || nflag)
  249.           if (!free) printf("**BUFFER FULL**\007\007");
  250.           else { *cptr++ = c; free--; }
  251.         if (chflag) {
  252.             checksum += c2;
  253.             bcount++;
  254.             if (bcount == SECSIZ) {
  255.               bcount = 0;
  256.               outmod(checksum >> 8);
  257.               outmod(checksum);
  258.               checksum = 0;
  259.               c = getmod();
  260.               if (c == EOT) {
  261.                 rdump(0); rclose();
  262.                 printf("\n%s received OK\n",rname);
  263.                 }
  264.           else if (c == ACK) {
  265.             if (cptr > buf+1000) rdump(0);
  266.             if (!didflag) printf("Good sector <%d>\n",++scount);
  267.             outmod(0xFD);
  268.            }
  269.           else  {
  270.             cptr -= SECSIZ;
  271.             free += SECSIZ;
  272.             printf("\nChecksum error. Retrying <%d>\n",scount+1);
  273.             outmod(0xFD);
  274.             timoutf = 0;
  275.           }
  276.  
  277.         }
  278.           }
  279.       }
  280.     }
  281.  
  282.     if (kbready()) {
  283.       c = getch();
  284.       if (c != SPECIAL) {
  285.         if (pflag || (!tflag && !(rflag && chflag))) {
  286.         outmod(c);
  287.         if (dodflag) display(c);
  288.           }
  289.         }
  290.       else special();
  291.       }
  292.     goto loop;
  293. }
  294.  
  295.  
  296. /*
  297.     Handle special Telnet command:
  298. */
  299.  
  300. special()
  301. {
  302.         char c;
  303.         int n;
  304.  
  305.         printf("\nSpecial: ");
  306.         if ( (c = getchar()) != '\n') printf("  ");
  307.         switch (toupper(c)) {
  308.         case '\n':  return;
  309.         case SPECIAL: outmod(SPECIAL);
  310.                   printf("Special char sent\n");
  311.                   break;
  312.  
  313.         case '7':  spflag = ask("Strip parity");
  314.                break;
  315.  
  316.         case 'N':  nflag = ask("Recognize incoming nulls");
  317.                break;
  318.  
  319.         case 'F':  fflag = ask("Transmit CR-LF pairs as CR only");
  320.                break;
  321.  
  322.         case 'H':  if (rflag || tflag)  { printf(
  323.                 "Must abort transfer first\n");
  324.                 break;
  325.                 }
  326.                printf("\nAre you either at half");
  327.                printf("-duplex or getting an ");
  328.                hdflag = ask ("echo");
  329.                reset();
  330.                break;
  331.  
  332.         case 'L':  lflag = ask("List incoming data");
  333.                break;
  334.  
  335.         case 'Z':  printf(CLEARS);
  336.                break;
  337.  
  338.         case 'P':  if (pflag) printf("Already pausing");
  339.                else if (!(tflag || rflag))
  340.                   printf("Not transmitting or receiving");
  341.                else {
  342.                 pflag = 1;
  343.                 dod_sav = dodflag;
  344.                 did_sav = didflag;
  345.                 dodflag = !hdflag;
  346.                 didflag = 1;
  347.                 printf("Ok, pausing from %s", tflag ?
  348.                         "transmission" : "collection");
  349.                }
  350.                goto lf;
  351.  
  352.         case 'R':  if (!pflag) printf("Not pausing");
  353.                else {
  354.                 pflag = 0;
  355.                 dodflag = dod_sav;
  356.                 didflag = did_sav;
  357.                 printf("%s now enabled again.", tflag ?
  358.                        "transmission" : "collection");
  359.                }
  360.                goto lf;
  361.  
  362.         case 'K':  printf("Text buffer !ZAPPED!");
  363.                free = bufspace;
  364.                cptr = buf;
  365.                goto lf;
  366.  
  367.         case 'V':  if (rflag) {
  368.                 putchar('\n');
  369.                 i = buf;
  370.                 while (i < cptr) putchar(*i++);
  371.                 printf("\n%u bytes free",free);
  372.                 }
  373.                else printf("No receiving file open");
  374.                goto lf;
  375.  
  376.         case 'O':  if (rflag) rclose();
  377.                if (tflag) tabort();
  378.                printf("\nOutput filename? ");
  379.                gets(rname);
  380.                rflag = 1;
  381.                if (!askstuff()) {
  382.                 rflag = 0;
  383.                 return;
  384.                }
  385.                printf("Creating %s...",rname);
  386.                rfd = fcreat(rname,rbuf);
  387.                if (rfd == ERROR) {
  388.                 printf("Cannot create %s\n",rname);
  389.                 reset();
  390.                 break;
  391.                 }
  392.                putchar('\n');
  393.                cptr = buf;
  394.                free = bufspace;
  395.                rflag = cflag = 1;
  396.                pflag = checksum = bcount = 0;
  397.                if (chflag) {
  398.                 printf("Trying to link...");
  399.                 do {
  400.                   c = getmod();
  401.                   if (abortf) {
  402.                     printf("aborting...\n");
  403.                     unlink(rname);
  404.                     reset();
  405.                     return;
  406.                   }
  407.                   timoutf = 0;
  408.                 } while (c & 0x7f);
  409.                 printf("linked.\n");
  410.                 outmod(0);
  411.                 }
  412.                break;
  413.  
  414.         case 'D':  if (rflag) rdump(1);
  415.                else printf("No output file");
  416.                goto lf;
  417.  
  418.         case 'C':  if (rflag) rclose();
  419.                else printf("No output file");
  420.                goto lf;
  421.  
  422.         case 'Q':  if (tflag) tabort();
  423.                if (rflag) rclose();
  424.                exit();
  425.  
  426.         case 'A':  if (tflag || rflag) {
  427.                        if (chflag) outmod(ETX);
  428.                      abortf = 1;
  429.                           break;
  430.                      }
  431.                printf("No transfer to abort.\n");
  432.                goto lf;
  433.  
  434.         case 'T':  if (tflag) tabort();
  435.                if (rflag) rclose();
  436.                printf("\nFile to transmit? ");
  437.                gets(tname);
  438.                tflag = 1;
  439.                if (!askstuff()) {
  440.                 tflag = 0;
  441.                 return;
  442.                }
  443.                tfd = open(tname,0);
  444.                if (tfd == ERROR) {
  445.                 printf("Cannot open %s\n",tname);
  446.                 reset();
  447.                 goto lf;
  448.                 }
  449.                pflag = checksum = bcount = 0;
  450.                if (read(tfd,tbuf,1) <=0) {
  451.                 printf("Read error from %s\n",
  452.                     tname);
  453.                 abortf = 1;
  454.                 return;
  455.                }
  456.                if (chflag) {
  457.                 printf("Trying to link...");
  458.                 while (1) {
  459.                  outmod(0);
  460.                  for (n=0; n<5000; n++)
  461.                 if (miready()) {
  462.                  if( !(getmod() & 0x7f)) {
  463.                     printf("linked.\n");
  464.                     return;
  465.                   }
  466.                  }
  467.                 else if (kbabort()) {
  468.                     printf("aborting.\n");
  469.                     return;
  470.                      }
  471.                  }
  472.                 }
  473.             break;
  474.  
  475.         case 'S':  dostat();
  476.                goto lf;
  477.  
  478.         default:   prcoms();
  479.  
  480.       lf:       putchar('\n');
  481.     }
  482. }
  483.  
  484. /*
  485.     Print out legal Telnet commands:
  486. */
  487.  
  488. prcoms()
  489. {
  490.     printf("\nBDS Telnet commands are:\n");
  491.     printf("Double SPECIAL: send SPECIAL\n");
  492.     printf("o: Open output file, start collection\n");
  493.     printf("p: Pause (suspend collection or transmission)\n");
  494.     printf("r: Resume after pausing\n");
  495.     printf("d: Dump (append) text buffer to output file\n");
  496.     printf("c: Close output file (after dumping buffer)\n");
  497.     printf("v: View contents of text buffer\n");
  498.     printf("k: Kill (erase) contents of text buffer\n");
  499.     printf("t: Transmit a file to modem\n");
  500.     printf("a: Abort transfer of file\n");
  501.     printf("n: accept or ignore Nulls\n");
  502.     printf("7: select policy regarding Parity bits\n");
  503.     printf("f: select whether to transmit CR-LF as just CR\n");
  504.     printf("h: set Half/full duplex mode\n");
  505.     printf("l: control CP/M List device\n");
  506.     printf("z: clear console terminal screen\n");
  507.     printf("s: display Status of Telnet\n");
  508.     printf("q: dump & close output file (if open) and Quit to CP/M");
  509. }
  510.  
  511.  
  512.  
  513. /*
  514.     Print opening message and initialize program:
  515. */
  516.  
  517. init()
  518. {
  519.     printf(TITLE);
  520.     timoutf = cflag = nflag = lflag = pflag = abortf = fflag = 0;
  521.     spflag = 1;
  522.     lastc = 0;
  523.     buf = &buf + 1;
  524.     bufspace = buf + 500 - topofmem();   /* compute space available */
  525.     bufspace = -bufspace;             /* for text collection buf */
  526.     printf("\n\nAnswer `y' if either your modem is set to half-duplex,\n");
  527.     printf("or you expect an echo from the system on the");
  528.     printf(" other end\n");
  529.     printf("of the line; else answer `n':\n");
  530.     hdflag = ask("Do you expect an echo");
  531.     reset();
  532.     printf("OK; you're on line...\n\n");
  533. }
  534.  
  535.  
  536. /*
  537.     Get all the info pertinent to a file transfer; i.e,
  538.     whether or not the file is text (and needs parity
  539.     stripped, nulls ignored, echoing to console, etc.),
  540.     whether or not checksumming and handshaking are
  541.     required (they always go together), and make sure
  542.     the user is in full duplex mode.
  543. */
  544.  
  545. askstuff()
  546. {
  547.     printf("\n%s ",rflag ? "receiving" : "transmitting");
  548.     if (ask("text (y) or binary data (n)  ")) {
  549.         nflag = 0;
  550.         spflag = didflag = 1;
  551.         dodflag = !hdflag;
  552.         printf("Stripping parity, ignoring nulls,\n");
  553.         printf("  %sdisplaying %s data.\n",
  554.         (rflag ? didflag : dodflag) ? "" : "not ",
  555.             rflag ? "incoming" : "outgoing");
  556.     }    
  557.     else {
  558.         spflag = didflag = dodflag = 0;
  559.         nflag = 1;
  560.         printf("%s all data verbatim, and not\n",
  561.             rflag ? "Receiving" : "Sending");
  562.         printf("displaying it on the console.\n");
  563.      }
  564.  
  565.     putchar('\n');
  566.     printf("Handshaking & checksumming can only happen\n");
  567.     printf("if the other computer has this same program\n");
  568.     printf("running. Do you want handshaking & checksumming");
  569.     chflag = ask("");
  570.     if (chflag && hdflag) {
  571.         printf("Can't do it unless you can eliminate");
  572.         printf(" the echo! Aborting.\n");
  573.         return 0;
  574.     }
  575.     fflag = shflag = ask("Do you want Ascii handshaking") ;
  576.     scount = 0;
  577.     return ask("OK...type y to begin, n to abort:");
  578. }
  579.  
  580. /*
  581.     Routine to print out a string and return true
  582.     if the user responds positively
  583. */
  584.  
  585. int ask(s)
  586. char *s;
  587. {
  588.     char c;
  589.     while (1)
  590.     {
  591.         printf("%s ",s);
  592.         printf("(y/n)? ");
  593.         c = toupper(getchar());
  594.         if (c == 'Y')
  595.         { 
  596.             printf("es\n");    
  597.             return 1;
  598.         }
  599.         else if (c == 'N')
  600.         {
  601.             printf("o\n");
  602.             return 0;
  603.         }
  604.         else putchar('\n');
  605.     }
  606. }
  607.  
  608.  
  609. /*
  610.     Print out state of Telnet program:
  611. */
  612.  
  613. dostat()
  614. {
  615.  
  616.        putchar('\n');
  617.  
  618.        if (rflag) {
  619.         printf("Output file = %s\n",rname);
  620.         printf("Text buffer has %u bytes free",
  621.             free);
  622.         printf("\nText collection: ");
  623.           if (cflag) if (pflag) printf("on, but pausing\n");
  624.                  else printf("on\n");
  625.           else printf("off\n");
  626.         }
  627.        else printf("No output file\n");
  628.     
  629.        if (tflag) {
  630.         printf("Transmitting: %s ",
  631.                 tname);
  632.         if (pflag) printf("(but pausing)");
  633.         putchar('\n');
  634.         }
  635.        else printf("Not transmitting any file\n");
  636.     
  637.        printf("Incoming nulls are being %s\n",
  638.          nflag ?"collected" : "ignored");
  639.     
  640.        printf("Parity bits are being %s\n",
  641.          spflag ?"stripped" : "preserved");
  642.     
  643.        printf("Half-duplex mode: %s",
  644.         hdflag ? "on" : "off");
  645. }
  646.  
  647.  
  648. /*
  649.     Routine to dump contents of the memory text buffer
  650.     to the output file and clear the buffer for more
  651.     data:
  652.     (Note that the "else putchar('\0');" clause may not
  653.     be necessary on your system; this is here only to
  654.     make up for a strange "feature" of Lifeboat's 
  655.     Northstar CBIOS where disk polling happens during
  656.     console output, potentially causing bytes to be 
  657.     missed from the modem.)
  658. */
  659.  
  660. rdump(n)
  661. {
  662.     for (i=buf; i<cptr; i++) putc(*i,rbuf);
  663.     cptr = buf;
  664.     free = bufspace;
  665.     if (n) printf("\nBuffer written\n");
  666.     else putchar('\0');
  667. }
  668.  
  669.  
  670. /*
  671.     Routine to dump and close the output file:
  672. */
  673.  
  674. rclose()
  675. {
  676.     rdump(1);
  677.     printf(" Closing %s ",rname);
  678.     if (!chflag) putc(CPMEOF,rbuf);
  679.     fflush(rbuf);
  680.     close(rfd);
  681.     reset();
  682.     putchar('\n');
  683. }
  684.  
  685. /*
  686.     Routine to reset telnet
  687. */
  688.  
  689. reset()
  690. {
  691.     timoutf = rflag = tflag = shflag = chflag = cflag = 0;
  692.     scount = 0;
  693.     spflag = 1;
  694.     dodflag = !hdflag;
  695.     didflag = 1;
  696. }
  697.  
  698.  
  699. /*
  700.     Get a byte from the modem:
  701. */
  702.  
  703. getmod()
  704. {
  705.     char c;
  706.     unsigned n;
  707.     if (timoutf) return;
  708.     for (n=20000; !miready() && n; n--)
  709.        if (kbabort()) return;
  710.     if (!n) {
  711.         timoutf = 1;
  712.         return 1;
  713.      }
  714.     c = mgetc();
  715.     return c;
  716. }
  717.  
  718.  
  719. /*
  720.     Output a byte to the modem:
  721. */
  722.  
  723.  
  724. outmod(c)
  725. char c;
  726. {
  727.     while (!moready())
  728.         if (kbabort()) return;
  729.     mputc(c);
  730. }
  731.  
  732. kbready()
  733. {
  734.     return bios(2);
  735. }
  736.  
  737.  
  738.  
  739. /*
  740.     Get a character from the keyboard:
  741.     (Uses a direct BIOS instead of going through
  742.     the BDOS. By naming this the same as the library
  743.     version of "getchar", we insure that THIS version
  744.     is used by things like "gets" instead of the library
  745.     version.)
  746. */
  747.  
  748. getchar()
  749. {
  750.     char c;
  751.     c = getch();
  752.     if (c == '\r') c = '\n';
  753.     putchar(c);
  754.     return c;
  755. }
  756.  
  757.  
  758. getch()
  759. {
  760.     return bios(3);
  761. }
  762.  
  763.  
  764.  
  765. /*
  766.     Return true if keyboard hit and SPECIAL
  767.     typed:
  768. */
  769.  
  770. kbabort()
  771. {
  772.     if (kbready() && getch() == SPECIAL) {
  773.         abortf = 1;
  774.         return 1;
  775.     }
  776.     return 0;
  777. }
  778.  
  779.  
  780. /*
  781.     Write a character to the console.
  782. */
  783.  
  784. putchar(c)
  785. char c;
  786. {
  787.     if (c == '\n') putch2('\r');
  788.     putch2(c);
  789. }
  790.  
  791. putch2(c)
  792. char c;
  793. {
  794.     bios(4,c);
  795. }
  796.  
  797.  
  798. /*
  799.     Write character to console display, and also to
  800.     system list device if that is enabled:
  801. */
  802.  
  803. display(c)
  804. char c;
  805. {
  806.     if (c==CPMEOF) return;
  807.     putch2(c);
  808.     if (lflag) bdos(5,c);
  809. }
  810.  
  811.  
  812. xmit()
  813. {
  814.     int incheck;
  815.     int n;
  816.     char c;
  817.     if (pflag || !moready()) return 0;
  818.     c = tbuf[bcount++];
  819.     checksum += c;
  820.     if ((!(spflag && (c&0x7f)==CPMEOF && !chflag)) &&
  821.         (!(!chflag && c=='\n' && lastc=='\r' &&
  822.           !nflag && fflag)))
  823.         outmod(c);
  824.     lastc = c;
  825.     if(shflag && (c == '\n')) pflag = TRUE ;
  826.     /* I need to have control over this later on... */
  827.     if(c == 0x1a) return TRUE ;
  828.     if (dodflag) display(c);
  829.     if (bcount != SECSIZ) return FALSE ;
  830.     bcount = 0;
  831.     if (!chflag) return !read1();
  832.     incheck = (getmod() << 8) + getmod();
  833.     if (incheck != checksum) {
  834.         for (n=0; n<20000; n++);     /* let line settle down */
  835.         printf("\nError. Resending sector %d...\n",scount+1);
  836.         outmod(NAK);
  837.     }
  838.     else if (read1()) {
  839.         if (!dodflag) printf("Good sector <%d>\n",++scount);
  840.         outmod(ACK);
  841.     }
  842.     else { outmod(EOT); return 1; }
  843.  
  844.     checksum = 0;
  845.     if (getmod() != 0xFD) {
  846.             printf("\nPhase error; aborting...");
  847.             abortf = 1;
  848.             }
  849.     return 0;
  850. }
  851.  
  852.  
  853. /*
  854.     Read a sector of the transmission file:
  855. */
  856.  
  857. read1()
  858. {
  859.     int i;
  860.     i = read(tfd, tbuf, 1);
  861.     if ( i == ERROR) {
  862.         printf("\nRead error from %s; Aborting.\n",
  863.             tname);
  864.         tabort();
  865.      }
  866.     return i;
  867. }
  868.  
  869. tabort()
  870. {
  871.     if (chflag)  while (bcount++ != 133) outmod(ETX);
  872.     printf("\nTransmission of %s aborted.\n",tname);
  873.      close(tfd);
  874.     reset();
  875. }
  876.